module CategoryHelper
  
  #  category_select_tree_tag_for_attr @article, "category", nil, {:class=>"select"}
  def category_select_tree_tag_for_attr(object, attr, selected = nil, options = {})
    @container = [['请选择 ','']]
    def children_iteration(childrens,num=2)
      childrens.children.each do |children|
        @container += [["#{'-' * num}"+children.name,children.id]]
        children_iteration(children,num+=2)
      end
    end
    Category.roots(object.class.to_s, attr).each do |item|
      @container += [[item.name,item.id]]
      children_iteration(item)
    end
    select_tag "#{object.class.to_s.tableize.singularize}[#{attr}_id]", options_for_select( @container, selected ), options
  end

  #  category_select_tree_tag_for_model_attr Article, "category", nil, {:class=>"select"}
  def category_select_tree_tag_for_model_attr(model, attr, selected = nil, options = {})
    @container = [['请选择 ','']]
    def children_iteration(childrens,num=2)
      childrens.children.each do |children|
        @container += [["#{'-' * num}"+children.name,children.id]]
        children_iteration(children,num+=2)
      end
    end
    Category.roots(model.to_s, attr).each do |item|
      @container += [[item.name,item.id]]
      children_iteration(item)
    end
    select_tag "category[parent_id]", options_for_select( @container, selected ), options
  end
  
end